/**
 * Created by Administrator on 2017/1/6.
 */

var UserToken = module.exports;
require('../util/common.js');
var redis_db = require('./redis_db.js');
var CONFIG = require('../config.js');

function generateRandom(uid) {
    return randomString(12) + '_' + uid;
};

UserToken.generateRandom = generateRandom;

function getAccountAndTime(userInfo) {
    var info = {};
    if(userInfo.isAdmin) {
        info.account = 'adm_'+userInfo.account;
        info.time = 1800;
    } else {
        info.account = 'user_'+userInfo.account;
        info.time = 48*3600;
    }
    return info;
}

function getToken(userInfo) {
    if(userInfo.isAdmin) {
        var uid = userInfo.uid + 'yyt';
    } else {
        var uid = userInfo.uid;
    }
    var token = generateRandom(uid);
    return token;
}

function setVal(token, account, data, time) {
    redis_db.set(token, data, time, function(err){
        if(err) {
            console.log(err);
            return;
        }
        console.log('admin_token insert redis');
    });
    redis_db.set(account ,token ,time, function (err) {
        if(err) {
            console.log(err);
        }
        console.log('token2info insert success');
    });
}

function expireTokenAndUserInfo(token, account, time) {
    redis_db.expire(account, time, function(err){
        if(err) {
            console.log(err);
        }
    });
    redis_db.expire(token, time, function(err){
        if(err) {
            console.log(err);
        }
    });
}

function delData(token, account) {
    if(token) {
        redis_db.del(token, function (err) {
            if(err) {
                console.log(err);
            }
        });
    }
    if(account) {
        redis_db.del(account, function(err){
            if(err) {
                console.log(err);
            }
        });
    }
    return;
}

UserToken.onLogin = function (userInfo, cb) {
    var info = getAccountAndTime(userInfo);
    return redis_db.get(info.account, function(err, result) {
        var token;
        if(err) {
            console.log(err);
            cb(err);
        }
        token = result;
        if(!token){
            var str = {};
            token = getToken(userInfo);
            if(userInfo.isAdmin) {
                str.time = info.time;
                str.uid = userInfo.uid;
                str.isAdmin = userInfo.isAdmin;
                str.account = userInfo.account;
                str = JSON.stringify(str);
            } else {
                str.time = info.time;
                str.uid = userInfo.uid;
                str.account = userInfo.account;
                str = JSON.stringify(str);
            }
            setVal(token, info.account, str, info.time);
            cb(null, token);
            return;
        }
        expireTokenAndUserInfo(token, info.account, info.time);
        cb(null, token);
        return;
    });
};

UserToken.checkLogin = function (token, cb) {
    if(token == undefined) {
        cb(null, false)
    } else {
        redis_db.get(token, function(err, result){
            if(err) {
                return;
            }
            if(result) {
                result = JSON.parse(result);
                cb(null, result);
                var info = getAccountAndTime(result);
                expireTokenAndUserInfo(token, info.account, info.time)
            } else {
                delData(token, undefined);
                cb(null, false);
            }
        });
    }
};

UserToken.onLogout = function (token ,cb) {
    return redis_db.get(token, function(err, result){
        if(err) {
            return;
        }
        if(result) {
            result = JSON.parse(result);
            var info = getAccountAndTime(result);
            delData(token, info.account);
            cb(null ,true);
        } else {
            cb(null ,false);
        }
    });
};

UserToken.checkAllLogin = function (token, identify, cb) {
    if (!token) {
        cb('token不为空', 400);
        return;
    }
    UserToken.checkLogin(token, function (err, userInfo) {
        if (err) {
            cb(err, 400);
            return;
        }
        if (!userInfo) {
            cb('请先登录', 403);
            return;
        }
        if (userInfo.isAdmin) {
            if (userInfo.role.indexOf('admin') < 0 && userInfo.role.indexOf('adm_') < 0 && userInfo.role.indexOf(identify+'_shop') < 0) {
                cb('无权限', 400);
                return;
            }
        }

        cb(null, 0, userInfo);
    });
};

//修改密码后生成新的token
UserToken.updateToToken = function (userInfo, cb) {
    var info = getAccountAndTime(userInfo);
    return redis_db.get(info.account, function(err, result){
        if(err) {
            return;
        }
        if(result) {
            delData(result, info.account);
        }

        var token = getToken(userInfo);
        var str = {};
        str.time = info.time;
        if(userInfo.isAdmin) {
            str.uid = userInfo.uid;
            str.isAdmin = userInfo.isAdmin;
            str.account = userInfo.account;
            str = JSON.stringify(str);
        } else {
            str.uid = userInfo.uid;
            str.account = userInfo.account;
            str = JSON.stringify(str);
        }
        setVal(token, info.account, str, info.time);
        cb(null, token);
        return;
    });
}


